package com.woniuxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniuxy.commons.exception.NoticeException;
import com.woniuxy.commons.utile.BeanCopyUtil;
import com.woniuxy.controller.dto.AccountDto;
import com.woniuxy.controller.dto.NoticeDto;
import com.woniuxy.controller.dto.showContent;
import com.woniuxy.controller.form.NoticeForm;
import com.woniuxy.controller.form.PageForm;
import com.woniuxy.dao.model.RepairNotice2;
import com.woniuxy.dao.RepairNotice2Dao;
import com.woniuxy.service.RepairNotice2Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import netscape.javascript.JSObject;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 通知公告 服务实现类
 * </p>
 *
 * @author Keep it simple
 * @since 2022-06-28
 */
@Service
public class RepairNotice2ServiceImpl extends ServiceImpl<RepairNotice2Dao, RepairNotice2> implements RepairNotice2Service {

    @Resource
    private RepairNotice2Dao repairNotice2Dao;

    @Resource
    private StringRedisTemplate srt;

    /**
     * 新增通知公告
     * @param noticeForm
     */
    @Override
    public void addNotice(NoticeForm noticeForm) {
        AccountDto principal = (AccountDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        noticeForm.setPublisher(principal.getAccountName());
        if (noticeForm.getNoticetitle()==null || noticeForm.getNoticetitle().equals("")) throw new  NoticeException(1006,"标题不能为空");
        Set<String> title = srt.opsForSet().members("title");
        if (title.contains(noticeForm.getNoticetitle())) throw new NoticeException(1006,"该标题已存在，新增失败");
        if (noticeForm.getNoticecontent()==null || noticeForm.getNoticecontent().equals("")) throw new NoticeException(1006,"正文不能空");
        if (noticeForm.getPublisher()==null || noticeForm.getPublisher().equals("")) throw new NoticeException(1006,"发布人不能空");
        RepairNotice2 notice2 = BeanCopyUtil.copyOne(noticeForm, RepairNotice2.class);
        repairNotice2Dao.insert(notice2);

//        RepairNotice2 rn = repairNotice2Dao.selectOne(new QueryWrapper<RepairNotice2>().eq("noticeTitle", noticeForm.getNoticetitle()));
//
//        String s=null;
//        try {
//           s = new ObjectMapper().writeValueAsString(rn);
//        } catch (JsonProcessingException e) {
//            e.printStackTrace();
//        }
//        //存标题，用于判断标题是否重复
//        srt.opsForSet().add("title",rn.getNoticetitle());
//
//        //存通知公告的具体数据，标题当key
//        srt.opsForHash().put("title",rn.getNoticetitle(),s);
    }

    @Override
    public RepairNotice2 test(String titel) {
        QueryWrapper<RepairNotice2> repairNotice2QueryWrapper = new QueryWrapper<>();
        repairNotice2QueryWrapper.eq("noticeTitle",titel);
        RepairNotice2 repairNotice2 = repairNotice2Dao.selectOne(repairNotice2QueryWrapper);
        return repairNotice2;
    }

    @Override
    public NoticeDto getTiltel(PageForm pageForm) {
        Page<RepairNotice2> page = new Page<>(pageForm.getPage(), 6);
        Page<RepairNotice2> repairNotice2Page = repairNotice2Dao.selectPage(page, new QueryWrapper<RepairNotice2>());
        long pages = repairNotice2Page.getPages();//获取多少页
        List<RepairNotice2> records = repairNotice2Page.getRecords();//获取当前页数据
        List<showContent> strings = new ArrayList<>();
        for (RepairNotice2 t:records){
            showContent showContent = BeanCopyUtil.copyOne(t, showContent.class);
            strings.add(showContent);
        }
        NoticeDto noticeDto = new NoticeDto();
        noticeDto.setPage(pages);
        noticeDto.setN(strings);
        return noticeDto;

//        List<RepairNotice2> repairNotice2s = repairNotice2Dao.selectList(new QueryWrapper<RepairNotice2>());
//        List<showContent> strings = new ArrayList<>();
//        for (RepairNotice2 t:repairNotice2s){
//            showContent showContent = BeanCopyUtil.copyOne(t, showContent.class);
//            strings.add(showContent);
//        }
//        return strings;
    }
}
